package br.edu.ufcg.lsd.wbis.ui.server;

import static br.edu.ufcg.lsd.wbis.ui.server.ControllerAccesser.getController;
import static br.edu.ufcg.lsd.wbis.ui.server.Util.getEquivalentLevel;
import br.edu.ufcg.lsd.wbis.control.user.User;
import br.edu.ufcg.lsd.wbis.ui.client.beans.UserInfo;
import br.edu.ufcg.lsd.wbis.ui.client.services.SetupService;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class SetupServiceImpl extends RemoteServiceServlet implements SetupService {

	private static final long serialVersionUID = -3488564705121118008L;

	/**
	 * {@inheritDoc}
	 */
	public void forbidRootCreation() {
		try {
			getController().forbidRootUserCreation();
		} catch (Throwable e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public UserInfo createRootUser(UserInfo info) {
		try {
			User rootUser = getController().addRootUser(info.getLogin(), info.getPassword(), info.getName(), info.getEmail());
			UserInfo newInfo = new UserInfo();
			newInfo.setLogin(rootUser.getLogin());
			newInfo.setName(rootUser.getName());
			newInfo.setEmail(rootUser.getEmail());
			newInfo.setLevel(getEquivalentLevel(rootUser.getLevel()));
			newInfo.setPassword(info.getPassword());
			newInfo.setAuthorized(rootUser.isAuthorized());
			return newInfo;
		} catch (Throwable e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

}
